package com.nd.android.u.ui.chatUiUtils;

import ims.utils.SdCardUtils;

import java.io.File;
import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.net.Uri;
import android.provider.Browser;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.common.StarAppCIConst;
import com.common.android.ui.gif.BitmapToolkit;
import com.common.android.utils.audio.AudioPlayer;
import com.common.android.utils.audio.TalkPopWindow;
import com.nd.android.u.allCommonUtils.ToastUtils;
import com.nd.android.u.controller.ChatConst.REQUEST_CODE;
import com.nd.android.u.ui.R;
import com.product.android.commonInterface.BaseCommonStruct;
import com.product.android.commonInterface.CIConst;
import com.product.android.commonInterface.CommonInterfaceManager;

import de.greenrobot.event.EventBus;

/**
 * 界面相关工具类
 * <br>Created 2014-6-26 下午5:59:04
 * @version  
 * @author   cb		
 * @see 	 
 */
public class ChatViewUtil {
	
	/**
	 * Creates a new instance of ChatViewUtil.
	 * <br>Created 2014-6-26 下午6:00:21
	 */
	private ChatViewUtil(){};
    /**
     * 打开网页
     * <br>Created 2014-6-26 下午5:59:32
     * @param context  context
     * @param url url
     * @author       cb
    */
    public static void openWebView(Context context,String url) {
        Uri uri = Uri.parse(url);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName());
        context.startActivity(intent);
    }
    
    /**
     * 打开相册 
     * <br>Created 2014-6-26 下午6:04:59
     * @param context 当前的界面
     * @author       cb
    */
    public static void openPhotoLibrary(Object context){
    	if(!SdCardUtils.isSdCardExist()){
			ToastUtils.display( R.string.chat_no_sdcard_image);
			return;
		}
    	if (context==null){
    		return;
    	}
    	Intent intent=null;
		try {
			intent = new Intent(Intent.ACTION_PICK,
	                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
		} catch (Exception e) {
			//在某些低版本的手机上，上述方法会崩溃
			intent = new Intent(Intent.ACTION_GET_CONTENT);
			intent.setType("image/*");
		}
		try {
			if (context instanceof Activity){
				((Activity)context).startActivityForResult(intent, REQUEST_CODE.REQUEST_PHOTO_LIBRARY);	
			}else if (context instanceof Fragment){
				((Fragment)context).startActivityForResult(intent, REQUEST_CODE.REQUEST_PHOTO_LIBRARY);	
			}
		} catch (ActivityNotFoundException e) {
			ToastUtils.display( R.string.chat_no_album);
		}
    }
    /**
     * 从录音文件中获取该录音文件的长度
     * <br>Created 2014-5-28 下午5:39:13
     * @return
     * @throws AudioFileNotFoundException
     * @author       cb
    */
    public static int getDurationFromFile(File file){
        int dura = AudioPlayer.getAudioDuration(file.getAbsolutePath());
        if(dura==-1){
            return 0;
        }
        dura = (int) Math.ceil(dura/1000f);
        if (dura>TalkPopWindow.MAX_RECORD_TIME){
            dura=TalkPopWindow.MAX_RECORD_TIME;
        }
        return dura;
    }
    
	/**
	 * 调整图片的尺寸
	 * <br>Created 2014-7-22 下午7:23:37
	 * @param parent 父容器
	 * @param key 资源
	 * @author       cb
	*/
	public static void resetImageViewParams(View img,Object key){
		Options options=new Options();
		options.inJustDecodeBounds=true;
		if (key instanceof String){
			BitmapFactory.decodeFile((String) key, options);	
		}else if (key instanceof Integer){
			BitmapFactory.decodeResource(img.getResources(), (Integer) key, options);
		}else if (key instanceof Bitmap){
			options.outWidth=((Bitmap) key).getWidth();
			options.outHeight=((Bitmap) key).getHeight();
		}
		ViewGroup.LayoutParams lp=img.getLayoutParams();
		if (lp==null){
			lp=new ViewGroup.LayoutParams(-1,-1);
		}
		int long_line=options.outHeight;
		int short_line=options.outWidth;
		if (options.outHeight<options.outWidth){
			long_line=options.outWidth;
			short_line=options.outHeight;
		}
		float scale=BitmapToolkit.getScale(options, long_line, short_line);
		lp.width=(int) (options.outWidth/scale);
		lp.height=(int) (options.outHeight/scale);
//		lp.width=options.outWidth;
//		lp.height=options.outHeight;
//		Log.d("image",lp.width+","+lp.height);
		img.setLayoutParams(lp);
		EventBus.getDefault().post(img);
	}
	
	/**
	 * 显示转发对话框
	 * <br>Created 2014-10-30 下午2:51:46
	 * @param activity 当前界面
	 * @param intent 动作
	 * @param title 标题
	 * @author  cb
	*/
	public static void showDlg(final Activity activity, final Intent intent,String title) {
		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		builder.setTitle(R.string.forwarding_msg).setMessage(
				String.format(Locale.getDefault(),
						activity.getString(R.string.forwarding_msg_tip),title));
		builder.setPositiveButton(R.string.ok, new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				activity.startActivity(intent);
				activity.finish();
			}
		});
		builder.setNegativeButton(R.string.cancel, null);
		builder.show();
	}
	
	/**
	 * 通知对应相关的界面进行群属性变更处理
	 * 
	 * <br>Created 2014-11-21 上午11:27:52
	 * @param id 群id或私聊uid
	 * @param name 对应新名字
	 * @param chatType 聊天项目类型 0私聊 ，1正式群聊。见com.product.android.commonInterface.chat.ChatConst
	 * @author       ChuXY
	 */
	public static void notifyChatItemAttributionChange(long id,String name, int chatType){
		if(id<=0||TextUtils.isEmpty(name)){
			return;
		}
		if(chatType<0||chatType>1){
			return;
		}
		int handleId=-1;
		switch (chatType) {
		case com.product.android.commonInterface.chat.ChatConst.CHAT_TYPE_PERSON:
			handleId=StarAppCIConst.CHAT_NOTIFY_USER_NAME;
			break;
		case com.product.android.commonInterface.chat.ChatConst.CHAT_TYPE_NORMAL_GROUP:
			handleId=StarAppCIConst.CHAT_NOTIFY_GROUP_NAME_210003;
			break;
		default:
			break;
		}
		BaseCommonStruct struct = new BaseCommonStruct();
		struct.sPara=name;
		struct.bPara=true;
		struct.lPara=id;
        int res = CommonInterfaceManager.INSTANCE
                    .CommonInterfaceID(CIConst.CHAT_MODELID, handleId, struct);
        struct.showError(res, "notifyMessage");
	}
}
